package mycollections.list;

import mycollections.MyConcurrentModificationException;
import java.util.*;

public class MyArrayList<E> implements RandomAccess, MyList<E> {
    private Object data[];
    private int size;
    private int modificationCount;

    private class MyIterator implements Iterator<E> {
        private int currentPosition;
        private int expectedModificationCount;

        private MyIterator() {
            expectedModificationCount = modificationCount;
            currentPosition = 0;
        }

        private void checkComodification(){
            if (expectedModificationCount != modificationCount)
                throw new MyConcurrentModificationException();
        }

        @Override
        public boolean hasNext() {
            checkComodification();
            return size!=currentPosition;
        }

        @Override
        public E next() {
            checkComodification();
            return (E)MyArrayList.this.data[currentPosition++];
        }

        @Override
        public void remove() {
            MyArrayList.this.remove(currentPosition);
            expectedModificationCount=modificationCount;
        }
    }

    public MyArrayList() {
        this(10);
    }

    public MyArrayList(int initialCapacity) {
        data = new Object[initialCapacity];
    }

    public MyArrayList(MyList<E> list) {
        data = new Object[list.size()];
        for (E e : list)
            data[size++] = e;
    }

    public void ensureCapacity(int minCapacity) {
        if (minCapacity<data.length)
            throw new IllegalArgumentException();
        Object oldData[] = data;
        int nextCapacity = Math.max(data.length >> 1, minCapacity);
        data = new Object[nextCapacity];
        System.arraycopy(oldData, 0, data, 0, oldData.length);
        modificationCount++;
    }

    private void checkCapacity(int nextSize) {
        if (nextSize == data.length) {
            ensureCapacity(nextSize);
        }

    }

    private void checkElementIndex(int index) {
        if (index < 0 || index >= size)
            throw new MyIndexOutOfBoundsException(index);
    }

    private void checkPositionIndex(int index) {
        if (index < 0 || index > size)
            throw new MyIndexOutOfBoundsException(index);
    }

    @Override
    public void add(E e) {
        checkCapacity(size + 1);
        data[size++] = e;
        modificationCount++;
    }

    @Override
    public void add(int index, E e) {
        checkPositionIndex(index);
        checkCapacity(size+1);
        System.arraycopy(data, index, data, index + 1, size - index );
        data[index] = e;
        size++;
        modificationCount++;
    }

    @Override
    public void addAll(Collection<? extends E> c) {
        addAll(size, c);
    }

    @Override
    public void addAll(int index, Collection<? extends E> c) {
        checkPositionIndex(index);
        checkCapacity(size + c.size());
        System.arraycopy(data, index, data, index + c.size(), size - index );
        for (E e : c) {
            data[index++] = e;
        }
        size+=c.size();
        modificationCount++;
    }

    @Override
    public E get(int index) {
        checkElementIndex(index);
        return (E) data[index];
    }

    @Override
    public E remove(int index) {
        checkElementIndex(index);
        E e = (E) data[index];
        System.arraycopy(data, index + 1, data, index, size - index-1);
        size--;
        modificationCount++;
        return e;
    }

    @Override
    public void clear() {
        modificationCount++;
        for (int i = 0; i < size; i++)
            data[i] = null;
        size = 0;
    }

    @Override
    public boolean isEmpty() {
        return size == 0;
    }

    @Override
    public void set(int index, E e) {
        checkElementIndex(index);
        data[index] = e;
        modificationCount++;
    }

    @Override
    public int indexOf(E o) {
        for (int i = 0; i < size; i++)
            if (data[i].equals(o))
                return i;
        return -1;
    }

    @Override
    public int size() {
        return size;
    }

    @Override
    public Object[] toArray() {
        return Arrays.copyOf(data, size);
    }

    @Override
    public Iterator<E> iterator() {
        return new MyIterator();
    }

    public void trimToSize(){
        Object oldData[]=data;
        data=new Object[size];
        System.arraycopy(oldData,0,data,0,size);
        modificationCount++;
    }
}
